<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>教师添加招聘</title>
    <script type="text/javascript" src="../../static/common/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../../static/common/jquery/jquery-1.11.1.min.js"></script>
    <link rel="stylesheet" href="../../static/common/layui/css/layui.css">
    <script src="../../static/common/axios.min.js"></script>
    <script src="../../static/common/vue.min.js"></script>
</head>
<script>
    var userId = ${sessionScope.currentUser.getId()};
    var teacher = {};
    var positionTypes = [];//所有岗位信息
    var company = [];  // 所有已通过审核的用人单位（渲染下拉框）

    // 获取该教师信息
    axios.get('/teacherInfo/findOneDetail/'+userId)
        .then(function (response) {
            console.log(response.data);
            teacher = response.data.data.teacherInfo;
            // 获取本学院全部岗位类别信息
            axios.get('/PositionType/findBatchBySchoolId/'+teacher.schoolid)
                .then(function (response) {
                    for (var i = 0;
                         i < response.data.data.length; i++) {
                        positionTypes[response.data.data[i].id] = response.data.data[i].posname;
                    }
                    console.log(positionTypes);
                    //有效岗位类别
                    var validPositionTypes = response.data.data;
                    var html = '';
                    for (var i in validPositionTypes) {
                        html += '<option value=' + validPositionTypes[i].id + '>' + validPositionTypes[i].posname + '</option>';
                    }
                    $('select[name=positiontype]').append(html);
                })
                .catch(function (response) {
                    console.log(response);
                });
        })
        .catch(function (msg) {
            console.log(msg);
        });

</script>
<body>
<div style="margin: 15px;">
    <form class="layui-form" action="">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>招聘信息</legend>
        </fieldset>
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red">*</span>标题</label>
                <div class="layui-input-inline">
                    <input name="title" class="layui-input" lay-verify="required">
                </div>
                <label class="layui-form-label"><span style="color: red">*</span>用人单位</label>
                <div class="layui-input-inline">
                    <select name="orgid" lay-filter="org" lay-search lay-verify="required">
                        <option value="">选择用人单位</option>
                    </select>
                </div>
                <span>若尚无用人单位记录，则点击<a href="/teacher/orgInfo?operateType=1&origin='/teacher/recruitInfos'" style="text-decoration: underline;color:blue;">添加用人单位信息</a><br/>添加单位信息完成后待
                    通过审核再添加就业信息</span>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">招聘类别</label>
                <div class="layui-input-inline">
                    <select name="type" lay-filter="type" lay-search>
                        <option value="">选择招聘类别</option>
                        <option value="1">实习招聘</option>
                        <option value="2">就业招聘</option>
                    </select>
                </div>
                <label class="layui-form-label">岗位类别</label>
                <div class="layui-input-inline">
                    <select name="positiontype" lay-filter="positionType" lay-search>
                        <option value="">选择岗位</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">工作城市</label>
                <div class="layui-input-inline">
                    <input name="workingcity" class="layui-input">
                </div>
                <label class="layui-form-label">起薪</label>
                <div class="layui-input-inline">
                    <input name="salaryllimit" class="layui-input" lay-verify="numOfSalary">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">工作内容</label>
                <div class="layui-input-block">
                    <textarea id="ta_ckeditor" class="ckeditor my-math" cols="20" rows="2"
                              style="overflow:auto"></textarea>
                    <script type="text/javascript">CKEDITOR.replace('ta_ckeditor');</script>
                </div>
                <%--<textarea id="ta_ckeditor" class="ckeditor my-math" cols="20" rows="2" style="overflow:auto"></textarea>--%>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">工作环境照片</label>
                <div class="layui-input-block">
                    <img src="" id="showWorkingPic" style="height: 100px;width: 100px">
                    <input type="file" name="fileUp" id="workingpic" class="layui-upload-file">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">其他说明</label>
                <div class="layui-input-block">
                    <input name="comment" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">招聘人数</label>
                <div class="layui-input-inline">
                    <input name="recruitnum" class="layui-input" lay-verify="numOfPerson">
                </div>
                <label class="layui-form-label"><span style="color: red">*</span>联系人</label>
                <div class="layui-input-inline">
                    <input name="contactperson" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label"><span style="color: red">*</span>联系电话</label>
                <div class="layui-input-inline">
                    <input name="phone" class="layui-input" >
                </div>
                <label class="layui-form-label"><span style="color: red">*</span>联系邮箱</label>
                <div class="layui-input-inline">
                    <input name="email" class="layui-input" >
                </div>
            </div>
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="save">提交</button>
                <button type="submit" class="layui-btn" lay-submit lay-filter="saveTemporary">暂存</button>
                <button type="submit" class="layui-btn" onclick="window.history.go(-1);">返回</button>
            </div>
        </form>
    </form>
</div>
<script src="../../static/common/layui/layui.js"></script>
<script>
    var myDate = new Date();
    layui.use(['form', 'jquery', 'layer', 'upload'], function () {
        var form = layui.form(),
            $ = layui.jquery;
        var image = {};
        //联动对象
        var linkage = {};
        //渲染下拉框
        linkage.formRender = function (obj, tip) {
            $(obj).empty();
            $(obj).append('<option value="">' + tip + '</option>');
            form.render('select');
        };
        // 初始数据不存在
        linkage.readNoData = function (obj,url) {
            axios.get(url)
                .then(function (res) {
                    var array = res.data.data;
                    var html = '';
                    for (var i in array) {
                        html += '<option value=' + array[i].id + '>' + array[i].name + '</option>';
                    }
                    $(obj).append(html);
                    form.render('select');
                })
                .catch(function (res) {
                    console.log(res);
                });
        };
        var university = 'form select[name=university]',
            school = 'form select[name=school]';
        //读取院校
        if ($(university).length) {
            linkage.formRender(university, "选择大学");
            linkage.readNoData(university,'/university/findAll');
            //读取学院
            if ($(school).length) {
                linkage.formRender(school, "选择学院");
                form.on('select(university)', function (data) {
                    linkage.formRender(school, "选择学院");
                    linkage.readNoData(school,'/school/findBath/'+data.value);
                });
            }
        }

        var vm = new Vue({
            el: '#Vue',
            data: {
                show: true,
                recruitinfo: {
                    publisherid: userId
                }
            }
        });

        //渲染公司企业下拉框
        $.ajax({
            type: "POST",
            url: "/orginfo/findByCondition/",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({state:3}),   // 只可选择已通过审核的用人单位
            dataType: "json",
            success: function (Response) {
                for (var i = 0; i < Response.data.length; i++) {
                    company[i] = Response.data[i];
                }
                var html = '';
                for (var i = 0; i < company.length;i++) {
                    html += '<option value=' + company[i].id + ' >' + company[i].fullname + '</option>';
                }
                $('form select[name=orgid]').append(html);
                form.render('select');
            },
            error: function (msg) {
                console.log("失败");
                console.log(msg);
            }
        });
        //首先渲染
        form.render();
        var workingpic;
        var da = new Date();
        var myDate = da.getFullYear() + "-" + (da.getMonth() + 1) + "-" + da.getDate() + " " + da.getHours() + ":" + da.getMinutes() + ":" + da.getSeconds();

        form.on('submit(save)', function (data) {
//            console.log(data.field);
            data.field.publisherid = userId;
            data.field.schoolid = teacher.schoolid;
            data.field.job = CKEDITOR.instances.ta_ckeditor.getData();
            data.field.publishdate = myDate;
            data.field.state = 2;
            data.field.workingpic = workingpic;
            axios.post('/recruitInfo/add',data.field)
                .then(function (response) {
                    console.log(response.data);
                    layer.msg(response.data.message, {icon:1,time: 1000}, function () {
                        if (response.data.success) {
                            window.history.go(-1);
                        }
                    });
                })
                .catch(function (response) {
                    console.log(response);
                });
            return false;
        });

        form.on('submit(saveTemporary)', function (data) {
            data.field.publisherid = userId;
            data.field.job = CKEDITOR.instances.ta_ckeditor.getData();
            data.field.schoolid = teacher.schoolid;
            data.field.publishdate = myDate;
            data.field.state = 1;
            data.field.workingpic = workingpic;
//            console.log(data.field);
            axios.post('/recruitInfo/add',data.field)
                .then(function (response) {
                    console.log(response.data);
                    layer.msg(response.data.message, {icon:1,time: 1000}, function () {
                        if (response.data.success) {
                            window.history.go(-1);
                        }
                    });
                })
                .catch(function (response) {
                    console.log(response);
                });
            return false;
        });

        form.verify({
            numOfSalary:function (data) {
                if(isNaN(data)&&data!=''){
                    layer.msg("起薪必须为数字",{icon:2,time:1000});
                }
            },
            numOfPerson:function (data) {
                if(isNaN(data)&&data!=''){
                    layer.msg("招聘人数必须为数字",{icon:2,time:1000});
                }
            }
        });

        layui.upload({
            url: '/upload/workingpic',
            success: function (response) {
                image.path = response.data;
                image.description = "工作环境";
                image.isencrypted = 2;//需要加密
                axios.post("/image/add", image)
                    .then(function (response) {
                        console.log(response.data);
                        workingpic = response.data.data;
                        axios.get("/image/find/" + response.data.data)
                            .then(function (response) {
                                $('#showWorkingPic').attr('src', response.data.data);
                            })
                            .catch(function (response) {
                                console.log(response);
                            });
                    })
                    .catch(function (response) {
                        console.log(response);
                    })

            },
            before: function () {
                console.log('工作环境上传中');
            }
        });

    });
</script>
</body>
</html>
